package qz_rpt

import java.text.SimpleDateFormat

class UserWorkController {

    def commonDataService
    def dynamicQueryService

    def index(Integer max) {
        def cellList = org.kdx.DataUtils.getCellList()
        def pageSizeList = org.kdx.DataUtils.getPageSizeList()

        [totalCount: 5, pageSize: 20, pageCurrent: 1, orderField: 'id', orderDirection: 'asc', pageSizeList: pageSizeList, cellList: cellList]
    }


    def userWork() {
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        params.stepName = params.stepName
        params.state = params.state

        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime() - 29))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        def userWorkSum = []
        def tableHeadList = []
        def userWork = []
        def pageSizeList = [10, 20, 50, 100]
        def totalCount = 0
        params.SYS_QUERY_NAME = 'userWorkStepList'
        def userWorkStepList = dynamicQueryService.queryForList(params)
        /*params.SYS_QUERY_NAME = 'userWorkLotList'
        def userWorkLotList = dynamicQueryService.queryForList(params)

        params.SYS_QUERY_NAME = 'userWorkStepList'
        def userWorkStepList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'userWorkPartNameList'
        def userWorkPartNameList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'userWorkGroupList'
        def userWorkGroupList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'userWorkEqpList'
        def userWorkEqpList = dynamicQueryService.queryForList(params)*/
        def userWorkSumQty = []
        if (params.EXPORT_CONTENT_NAME == 'Excel') {
            params.SYS_QUERY_NAME = 'userWork'
            userWork = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (userWork.size() > 0) {
                tableHeadList = userWork[0].keySet()
            }
            params.SYS_QUERY_NAME = 'userWorkCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'userWorkSum'
            userWorkSum = dynamicQueryService.queryForList(params)
            params.SYS_QUERY_NAME = 'userWorkSumQty'
            userWorkSumQty = dynamicQueryService.queryForList(params)

        }


        render(view: 'userWork', model: [totalCount  : totalCount, tableHeadList: tableHeadList,
                                         pageSizeList: pageSizeList, pageSize: pageSize,
                                         startno     : params.startno ?: '', endno: params.endno ?: '',
                                         partName    : params.partName ?: '', userWork: userWork,
                                         stepName    : params.stepName ?: '', startTime: params.startTime ?: '', endTime: params.endTime ?: '',
                                         stepDesc    : params.stepDesc ?: '', userName: params.userName ?: '', department: params.department ?: '',
                                         lotId       : params.lotId ?: '', eqp: params.eqp ?: '', user: params.user ?: '', group3: params.group3 ?: '',
                                         /*userWorkLotList:userWorkLotList,userWorkUserList:userWorkUserList,
                                         userWorkStepList:userWorkStepList,userWorkPartNameList:userWorkPartNameList,
                                         userWorkGroupList:userWorkGroupList,userWorkEqpList:userWorkEqpList,*/
                                         userWorkSum : userWorkSum, userWorkSumQty: userWorkSumQty, step: params.step ?: '', userWorkStepList: userWorkStepList
        ])

    }


}
